"""
    a3mall （一）
    秒杀的业务
"""

import logging

import allure
import pytest
from Tools.logger import logger
from Tools.tools import ApiClient
from utils.load_data import *
from Tools.mysql import db

jdbc = db(
    host='10.59.9.12',
    password='yidong666',
    database='a3mall',
    user='root'
)

@pytest.fixture
def client():
    logger()
    return ApiClient("http://10.59.9.12:7001/")

class Test_a3mall():
    token = None
    spid = None
    # 登录
    @allure.step('秒杀的业务')
    @pytest.mark.parametrize('data1', read_yaml(r'C:\Users\张震海\Desktop\a3mall\data\a3mall.yaml'))
    def test_login_success(self, client, data1):
        """测试登录成功"""
        logger()
        logging.info('开始')
        data = {'username': data1.get('username'), 'password': data1.get('password')}
        response = client.post(path="api/public/login", json_data=data)
        a = jdbc.select("SELECT username FROM mall_users WHERE mobile = '19064013675'")
        print(a[0][0])
        assert '19064013675' == a[0][0]
        assert response.status_code == 200
        try:
            assert response.elapsed.total_seconds() <= 0.05
        except:
            print('实际时间为：', response.elapsed.total_seconds())
        result = response.json()
        assert result['info'] == data1.get('exp')
        try:
            Test_a3mall.token = 'Bearer ' + result['data']['token']
        except:
            print('token值为：',Test_a3mall.token)

        print("登录成功:", result)
    # 秒杀列表
    def test_second_kill(self,client):
        response = client.get(path='/api/second?page=1&type=0&sort=1')
        Test_a3mall.spid = response.json()['data']['list'][1]['id']
        # print(Test_a3mall.spid)
        print('秒杀列表:',response.json())
        assert response.status_code == 200
        try:
            assert response.elapsed.total_seconds() <= 0.05
        except:
            print('实际时间为：',response.elapsed.total_seconds())
    #商品详情
    def test_shop(self,client):
        response = client.get(path=f'/api/second/view?id={Test_a3mall.spid}')
        a=response.json()['data']['goods']['goods_id']
        d=response.json()['data']['goods']['title']
        assert d == '钻石项链'
        print('商品详情:',response.json())
        try:
            assert response.elapsed.total_seconds() <= 0.05
        except:
            print('实际时间为：',response.elapsed.total_seconds())
        assert response.status_code == 200
        b = jdbc.select("SELECT id FROM mall_goods WHERE title = '钻石项链'")
        print(b[0][0])
        assert a == b[0][0]
        c = jdbc.select("SELECT title FROM mall_goods WHERE id = 32")
        print(c[0][0])
        assert d == c[0][0]

    # 创建订单
    def test_create(self,client):
        headers = {'auth-token':Test_a3mall.token}
        responses = client.get(path=f'/api/order/confirm?id={Test_a3mall.spid}&type=second&sku_id=&num=1&shipping_type=1',headers=headers)
        print('创建订单',responses.json())
        try:
            assert responses.elapsed.total_seconds() <= 0.05
        except:
            print('实际时间为：',responses.elapsed.total_seconds())
        assert responses.status_code == 200

    # 支付
    def test_pay(self,client):
        headers = {'auth-token': Test_a3mall.token}
        data = {
  "id": Test_a3mall.spid,
  "type": "second",
  "address_id": 61,
  "store_id": 1,
  "shipping_type": 1,
  "bonus_id": "0",
  "payment": "balance",
  "remarks": "",
  "source": 1,
  "url": "http://10.59.9.12:7001/wap/pages/cart/confirm?id=5&sku_id=1&num=1&type=second",
  "sku_id": "1",
  "num": "1"
}
        responses = client.post(path='/api/order/create',json_data=data,headers=headers)
        print('支付',responses.json())
        try:
            assert responses.elapsed.total_seconds()<=0.05
        except:
            print('实际时间为：',responses.elapsed.total_seconds())
        assert responses.status_code == 200
        assert responses.json()['data']['msg'] == '支付成功'
        a = jdbc.select("SELECT id FROM mall_order WHERE mobile = '19064013675' ORDER BY id DESC LIMIT 1;")
        assert a[-1][0] ==responses.json()['data']['order_id']


if __name__ == '__main__':
    pytest.main()